(function () {
    window.Block = function () {
        //罗列所有类型
        var allType = ["S", "Z", "J", "L", "O", "I", "T"];
        //得到一种类型
        this.type = allType[parseInt(Math.random() * allType.length)];
        //得到一种类型中的总数量
        this.allDir = fangkuai[this.type].length;
        //随机得到一种
        this.dir = parseInt(Math.random() * this.allDir);
        //得到随机方块
        this.code = fangkuai[this.type][this.dir]
        //初始行
        this.row = 0
        //初始列
        this.col = 4
    }
    Block.prototype.render = function () {
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                if (this.code[i][j] != 0) {
                    game.setColor(i + this.row, j + this.col, this.code[i][j])
                }
            }
        }
    }
    //预判断方法，判断当前方块与对应位置的方块是否有重合
    Block.prototype.check = function (row, col) {
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                if (this.code[i][j] != 0 && game.map.codeMap[row + i][col + j] != 0) {
                    return false;
                }
            }
        }
        return true;
    }
    //方块进行下落之前需要进行预判断，判断方块下落的下一个位置是否有方块存在，如果没有方块存在就直接row++，否则停止下落
    Block.prototype.checkDown = function () {
        //下落之前，先进行预判断
        if (this.check(this.row + 1, this.col)) {
            this.row++;
        } else {
            //渲染预览框方块
            game.block = game.nextBlock;
            //渲染新的方块
            game.nextBlock = new Block()
            this.renderMap()
            //判断是否可以消行
            game.map.checkRemove()
            //判断游戏是否结束
            this.checkOver()
        }
    }
    //判断是否可用左移
    Block.prototype.checkLeft = function () {
        if (this.check(this.row, this.col - 1)) {
            this.col--;
        }
    }
    //判断是否可用右移
    Block.prototype.checkRight = function () {
        if (this.check(this.row, this.col + 1)) {
            this.col++;
        }
    }
    //将下落的方块渲染到地图中
    Block.prototype.renderMap = function () {
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                if (this.code[i][j] != 0) {
                    game.map.codeMap[this.row + i][this.col + j] = this.code[i][j];
                }
            }
        }
    }

    //一键到底
    Block.prototype.checkBlockEnd = function () {
        while (this.check(this.row + 1, this.col)) {
            this.row++;
        }
    }

    //变化方块
    Block.prototype.blockRot = function () {
        //保留旧的状态
        var oldDir = this.dir;
        this.dir++;
        if (this.dir > this.allDir - 1) {
            this.dir = 0;
        }
        //改变方向之后渲染新的方向
        this.code = fangkuai[this.type][this.dir];
        //判断当前改变后的方块是否有能力继续往下走
        if (!this.check(this.row, this.col)) {
            this.dir = oldDir;
            this.code = fangkuai[this.type][this.dir];
        }
    }

    //判断游戏结束
    Block.prototype.checkOver = function () {
        for (let i = 0; i < game.col; i++) {
            if (game.map.codeMap[0][i] != 0) {
                clearInterval(game.timer);
                alert("游戏结束了！！！您当前得分为：" + game.score)
            }
        }
    }
})()